package de.justphil.tcg.tcgandroidclient.gui.gameboard;

import de.justphil.tcg.tcgandroidclient.R;
import de.justphil.tcg.tcgandroidclient.content.CardData;
import de.justphil.tcg.tcgandroidclient.content.view.ViewCard;
import de.justphil.tcg.tcgandroidclient.gui.CardAttribute;
import de.justphil.tcg.tcgandroidclient.msgdata.Progress;
import de.justphil.tcg.tcgandroidclient.util.TCGLog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Card extends LinearLayout {
	
	public static final int STATUS_NORMAL = 1;
	public static final int STATUS_TURN = 2;
	public static final int STATUS_DEAD = 3;
	public static final int STATUS_EMPTY = 4;
	private CardAttribute life;
	private CardAttribute attack;
	private CardAttribute defense;
	private CardAttribute agility;
	private ImageView cardImg;
	private LinearLayout baseProperties;
	private LinearLayout addedProperties;
	private LinearLayout body;
	private View content;
	private int status;
	
	public Card(Context context, AttributeSet attrs) {
	
	
		super(context, attrs);
		LayoutInflater inflater =
				(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		content = inflater.inflate(R.layout.card, this);
		init(attrs);
	}
	
	public void setImage(Bitmap bmp) {
	
		cardImg.setImageBitmap(bmp);
	}
	
	public void setLife(int value) {
	
		life.setValue(value);
	}
	
	public void setBaseLife(int value) {
	
		life.setBaseValue(value);
	}
	
	public void setAttack(int value) {
	
		attack.setValue(value);
	}
	
	public void setBaseAttack(int value) {
	
		attack.setBaseValue(value);
	}
	
	public void setDefense(int value) {
	
		defense.setValue(value);
	}
	
	public void setBaseDefense(int value) {
	
		defense.setBaseValue(value);
	}
	
	public void setAgility(int value) {
	
		agility.setValue(value);
	}
	
	public void setBaseAgility(int value) {
	
		agility.setBaseValue(value);
	}
	
	public int getStatus() {
	
		return status;
	}
	
	public void setStatus(int status) {
	
		TCGLog.d(new Object[] {
				"SETTING STATUS to ", status });
		switch (status) {
			case STATUS_EMPTY:
				body.setVisibility(INVISIBLE);
				this.status = STATUS_EMPTY;
				TCGLog.d(new Object[] {
					"Setting card status to EMPTY." });
				break;
			case STATUS_DEAD:
				TCGLog.d(new Object[] {
					"Setting card status to DEAD." });
				body.setVisibility(VISIBLE);
				this.status = STATUS_DEAD;
				cardImg.setBackgroundResource(R.color.card_img_bg_dead);
				break;
			case STATUS_NORMAL:
				TCGLog.d(new Object[] {
					"Setting card status to NORMAL." });
				body.setVisibility(VISIBLE);
				this.status = STATUS_NORMAL;
				cardImg.setBackgroundResource(R.color.card_img_bg_normal);
				break;
			case STATUS_TURN:
				TCGLog.d(new Object[] {
					"Setting card status to TURN." });
				body.setVisibility(VISIBLE);
				this.status = STATUS_TURN;
				cardImg.setBackgroundResource(R.color.card_img_bg_turn);
				break;
			default:
				break;
		}
	}
	
	public void clearProperties() {
	
		TCGLog.d(new Object[] {
			"Clearing properties." });
		int childCount = baseProperties.getChildCount();
		if (addedProperties.getChildCount() != childCount) {
			TCGLog.e(new Object[] {
				"The base Properties and the added Properties must contain the same count of ImageViews!" });
			return;
		}
		for (int i = 0; i < childCount; i++) {
			baseProperties.getChildAt(i).setVisibility(GONE);
			addedProperties.getChildAt(i).setVisibility(GONE);
		}
	}
	
	public void addBaseProperty(int type, Bitmap bmp) {
	
		TCGLog.d(new Object[] {
			"Adding base property." });
		addProperty(type, bmp, baseProperties);
	}
	
	public void addAddedProperty(int type, Bitmap bmp) {
	
		TCGLog.d(new Object[] {
			"Adding added property." });
		addProperty(type, bmp, addedProperties);
	}
	
	public void removeProperty(int type) {
	
		/* Searching at first in the added properties. */
		{
			View property;
			int childCount = addedProperties.getChildCount();
			for (int i = 0; i < childCount; i++) {
				property = addedProperties.getChildAt(i);
				if (property.getVisibility() == VISIBLE && property.getId() == type) {
					property.setVisibility(GONE);
					TCGLog.d(new Object[] {
						"Removing added property." });
					return;
				}
			}
		}
		/* if no property has been found the base properties are searched too. */
		{
			View property;
			int childCount = baseProperties.getChildCount();
			for (int i = 0; i < childCount; i++) {
				property = baseProperties.getChildAt(i);
				if (property.getVisibility() == VISIBLE && property.getId() == type) {
					property.setVisibility(GONE);
					TCGLog.d(new Object[] {
						"Removing base property." });
					return;
				}
			}
		}
		TCGLog.e(new Object[] {
			"Trying to remove a Property which has never been added!" });
	}
	
	public void updateProperty(int type, Bitmap bmp) {
	
		if (bmp == null) {
			TCGLog.d(new Object[] {
				"Added null property image." });
			return;
		}
		/* Searching at first in the added properties. */
		{
			ImageView property;
			int childCount = addedProperties.getChildCount();
			for (int i = 0; i < childCount; i++) {
				property = (ImageView) addedProperties.getChildAt(i);
				if (property.getVisibility() == VISIBLE && property.getId() == type) {
					property.setImageBitmap(bmp);
					TCGLog.d(new Object[] {
							"Updating added property image of size ", bmp.getWidth(), "x",
							bmp.getHeight(), "." });
					return;
				}
			}
		}
		/* if no property has been found the base properties are searched too. */
		{
			ImageView property;
			int childCount = baseProperties.getChildCount();
			for (int i = 0; i < childCount; i++) {
				property = (ImageView) baseProperties.getChildAt(i);
				if (property.getVisibility() == VISIBLE && property.getId() == type) {
					property.setImageBitmap(bmp);
					TCGLog.d(new Object[] {
							"Updating base property image of size ", bmp.getWidth(), "x",
							bmp.getHeight(), "." });
					return;
				}
			}
		}
		TCGLog.e(new Object[] {
			"Trying to update a Property which has never been added!" });
	}
	
	public void updateData(CardData data) {
	
		setImage(data.getImage());
		setBaseLife(data.getLife());
		setBaseAttack(data.getAttack());
		setBaseDefense(data.getDefense());
		setBaseAgility(data.getAgility());
	}
	public void updateData(ViewCard viewCard) {
		
		setImage(viewCard.getImage());
		setBaseLife(viewCard.getLife());
		setBaseAttack(viewCard.getAttack());
		setBaseDefense(viewCard.getDefense());
		setBaseAgility(viewCard.getAgility());
	}
	
	public void updateValues(Progress progress) {
	
		setLife(progress.getLife());
		setAttack(progress.getAttack());
		setDefense(progress.getDefense());
		setAgility(progress.getAgility());
	}
	
	private void init(AttributeSet attrs) {
	
		if (!this.isInEditMode()) {
			/* reading the components */
			{
				cardImg = (ImageView) content.findViewById(R.id.cardImg);
				life = (CardAttribute) content.findViewById(R.id.cardLife);
				attack = (CardAttribute) content.findViewById(R.id.cardAttack);
				defense = (CardAttribute) content.findViewById(R.id.cardDefense);
				agility = (CardAttribute) content.findViewById(R.id.cardAgility);
				baseProperties = (LinearLayout) content.findViewById(R.id.base_properties);
				addedProperties = (LinearLayout) content.findViewById(R.id.added_properties);
				body = (LinearLayout) content.findViewById(R.id.card_body);
			}
		}
	}
	
	private void addProperty(int type, Bitmap bmp, LinearLayout properties) {
	
		int childCount = properties.getChildCount();
		ImageView property = null;
		int i = 0;
		for (; i < childCount; i++) {
			property = (ImageView) properties.getChildAt(i);
			if (property.getVisibility() == GONE) {
				property.setId(type);
				property.setImageBitmap(bmp);
				property.setBackgroundColor(Color.WHITE);
				property.setVisibility(VISIBLE);
				break;
			}
		}
		if (i >= childCount) {
			TCGLog.e(new Object[] {
				"Trying to add more Properties to a card than possible!" }, new Exception());
		}
		if (bmp == null) {
			TCGLog.d(new Object[] {
				"Added null property image." });
		}
		else {
			TCGLog.d(new Object[] {
					"Added property image of size ", bmp.getWidth(), "x", bmp.getHeight(), "." });
		}
	}
}
